package cn.tedu.dao.impl;

import java.util.List;

import cn.tedu.dao.RoleDao;
import cn.tedu.dao.common.CommonDao;
import cn.tedu.entity.Role;
import cn.tedu.vo.CountVO;
/**
 * RoleDao接口的子实现一
 * @author Administrator
 *
 */
public class RoleDaoImpl implements RoleDao {

	@Override
	public int getCount(String rolekeyword)throws Exception {
		int count=0;
		String sql="select count(role_id) geshu from t_role "
				+ "where role_name like ?";
		List<CountVO> countVOs=CommonDao.executeQuery(CountVO.class, sql, "%"+rolekeyword+"%");
		if(countVOs!=null && countVOs.size()==1) {
			count=countVOs.get(0).getGeshu();
		}
		return count;
	}

	@Override
	public List<Role> getRolesByPage(int currentPage, int pageSize, String rolekeyword)throws Exception {
		List<Role> roles=null;
		String sql="select role_id id,role_name name from t_role "
				+ "where role_name like ? "
				+ "limit ?,?";
		Object[] params=new Object[] {
				"%"+rolekeyword+"%",
				(currentPage-1)*pageSize,
				pageSize
		};
		roles=CommonDao.executeQuery(Role.class, sql, params);
		return roles;
	}

	@Override
	public int addRole(Role role) throws Exception {
		int rowAffect=0;
		String sql="insert into t_role(role_id,role_name) values(?,?)";
		rowAffect=CommonDao.executeUpdate(sql, role.getId(),role.getName());
		return rowAffect;
	}

	@Override
	public int deleteRole(String roleId) throws Exception {
		int rowAffect=0;
		String sql="delete from t_role where role_id=?";
		rowAffect=CommonDao.executeUpdate(sql, roleId);
		return rowAffect;
	}

	@Override
	public int updateRole(Role role)throws Exception {
		int rowAffect=0;
		String sql="update t_role set role_name=? where role_id=?";
		rowAffect=CommonDao.executeUpdate(sql, role.getName(),role.getId());
		return rowAffect;
	}

	@Override
	public List<Role> findAllRoles() throws Exception {
		List<Role> roles=null;
		String sql="select role_id id,role_name name from t_role";
		roles=CommonDao.executeQuery(Role.class, sql);
		return roles;
	}

}
